//
//  SettingsViewController.m
//  iEPD
//
//  Created by xavi on 12/29/12.
//  Copyright (c) 2012 Mobioneer Ltd. All rights reserved.
//

#import "SettingsViewController.h"
#import "InformationViewController.h"
#import "PassCodeViewController.h"

#import <Twitter/Twitter.h>
#import <Twitter/TWRequest.h>
#import <Twitter/TWTweetComposeViewController.h>

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MFMessageComposeViewController.h>

@interface SettingsViewController () <UIActionSheetDelegate>

@end

@implementation SettingsViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem.image = [UIImage imageNamed:@"ic_tabbar_settings"];
        self.title = NSLocalizedString(@"TITLE_TAB3", nil);
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"TITLE_TAB3", nil);
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.opaque = NO;
    self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
}

#pragma mark UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else return  3;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    if (indexPath.section == 0) {
        cell.textLabel.text = NSLocalizedString(@"ChangPasscode", nil);
    }
    else if (indexPath.section == 1)
    {
        if (indexPath.row == 0) {
            cell.textLabel.text = NSLocalizedString(@"SHARE", nil);
        }
        else if (indexPath.row == 1)
            cell.textLabel.text = NSLocalizedString(@"HELP", nil);
        else if (indexPath.row == 2)cell.textLabel.text = NSLocalizedString(@"ABOUT", nil);
    }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) return NSLocalizedString(@"CONFIGURATION", nil);
    else return NSLocalizedString(@"INFORMATION", nil);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        PassCodeViewController *passcode = [[PassCodeViewController alloc] init];
        passcode.changePass = @"CHANGEPASS";
        UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:passcode];
        [self.navigationController presentModalViewController:navi1 animated:YES];
    }
    if (indexPath.section == 1 )
    {
        if(indexPath.row > 0) {
        self.indexSetting = indexPath.row;
            InformationViewController *infoVC = [[ InformationViewController alloc] init];
            infoVC.indexSelectedOfSetting = self.indexSetting;
            [self.navigationController pushViewController:infoVC animated:YES];
        }
        else {
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(@"CANCEL", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"FACEBOOK", nil),NSLocalizedString(@"TWITTER", nil),NSLocalizedString(@"MAIL", nil), nil];
            [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
        }
    }
}

#pragma mark  - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            NSLog(@"Unsupport");
            break;
        case 1:
        {
                TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
            
                //Customize the tweet sheet here
                //Add a tweet message
            [tweetSheet setInitialText:@"This is a great application: iEPD. Get it here: https://itunes.apple.com/vn/app/photo-safe-pro-by-my-eyes-only/id402544816?mt=8"];
            
                //Set a blocking handler for the tweet sheet
                tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result){
                [self dismissModalViewControllerAnimated:YES];
            };
            
            //Show the tweet sheet!
            [self presentModalViewController:tweetSheet animated:YES];
        }
            break;
        case 2:
        {
            if ([MFMailComposeViewController canSendMail])
            {
                MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
                mailer.mailComposeDelegate = self;
                [mailer setSubject:@"Try this application!"];
                NSArray *toRecipients = [NSArray arrayWithObjects:@"", nil];
                [mailer setToRecipients:toRecipients];
//                UIImage *myImage = [UIImage imageNamed:@"photo@2x.png"];
//                NSData *imageData = UIImagePNGRepresentation(myImage);
//                [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"photo@2x"];
                NSString *emailBody = @"I like this application and I think you should try it too! https://itunes.apple.com/vn/app/photo-safe-pro-by-my-eyes-only/id402544816?mt=8";
                [mailer setMessageBody:emailBody isHTML:YES];
                [self presentModalViewController:mailer animated:YES];
            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ERROR", nil)
                                                                message:NSLocalizedString(@"NO_MAIL", nil)
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles: nil];
                [alert show];
            }
        }
            break;
        default:
            break;
    }
}

#pragma mark - MFMailComposeViewControllerDelegate

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
